﻿<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
    <title>Tree Demo</title>

    <!-- Bootstrap -->
    <link href="css/bootstrap.min.css" rel="stylesheet">

    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
	<![endif]-->
	<script>
		function createNode(id,name,description,keyword,sl,zc,tj){
			var node=new Object;
			node.id=id;
			node.name=name;
			node.description=description;
			node.keyword=keyword;
			node.sl=sl;
			node.zc=zc;
			node.tj=tj;
			node.child=new Array();
			node.parent=null;
			/*
			node.isleaf=function(){return this.child.length==0?true:false;};
			node.toString=function(){
				var tmp=this.id+this.name;
				return tmp;
			};
			*/
			node.toDOM=function(){
				var dom=document.createElement("div");
				dom.setAttribute("data-role","collapsible");
				dom.setAttribute("data-collapsed","true");
				dom.setAttribute("id","d"+this.id);
				dom.setAttribute("class","ui-collapsible ui-collapsible-inset ui-corner-all ui-collapsible-themed-content");
				var headline=document.createElement("h4");
				headline.setAttribute("class","ui-collapsible-heading");
				headline.appendChild(document.createTextNode("("+this.id.slice(-2)+")"+this.name));
				dom.appendChild(headline);
				if(this.description!=undefined)
					dom.appendChild(document.createTextNode(this.description));
				/*	
				if(this.sl!=undefined) 
					dom.appendChild(document.createElement("p").appendChild(document.createTextNode(this.sl)));
				*/
				if(this.zc!=undefined){
					dom.appendChild(document.createElement("p").appendChild(document.createTextNode(this.zc)));
				}
				if(this.tj!=undefined) 
					dom.appendChild(document.createElement("p").appendChild(document.createTextNode(this.tj)));
				return dom;
			};
			return node;
		};
		function createNodeOrder(id,col1,col2){
			var nodeorder=new Object;
			nodeorder.id=id;
			nodeorder.col1=col1;
			nodeorder.col2=col2;
			return nodeorder;
		}
		
		function query(str){
			var temp=new Array();
			for(var i=0;i<tree.length;i++){
				if(tree[i].name.indexOf(str)!=-1||tree[i].description.indexOf(str)!=-1){
					var order=createNodeOrder(i,tree[i].name.indexOf(str),tree[i].description.indexOf(str))
					temp.push(order);
				}
			}
			var queryresult=temp.sort(nodesort);
			return queryresult;
		}
		function nodesort(a,b){
			if(a.col1==-1)
				a.col1=Number.MAX_VALUE;
			if(a.col2==-1)
				a.col2=Number.MAX_VALUE;
			if(b.col1==-1)
				b.col1=Number.MAX_VALUE;
			if(b.col2==-1)
				b.col2=Number.MAX_VALUE;
			if(a.col1!=b.col1)
				return a.col1-b.col1;
			else if(a.col2!=b.col2)
				return a.col2-b.col2;
			else
				return 0;
		}
		function search(){
			var searchtext=$("#searchText").val();
			$("#result").empty();
			switch(searchtext.length)
			{
				case 0:
					var subset=document.createElement("div");
					subset.setAttribute("data-role","collapsible-set");
					for(var i=0;i<tree[0].child.length;i++){
						 subset.appendChild(tree[tree[0].child[i]].toDOM(false));
					}
					$(subset).appendTo("#result").trigger("create");
					jQuery.mobile.changePage("#resultpage");
					break;
				case 1:
					alert("请输入两个以上字符，或者不输入任何字符！\n不输入任何字符可以分类查看");
					  break;
				  default:
					  var dom=document.createElement("ul");
					  dom.setAttribute("data-role","listview");
					  dom.setAttribute("data-inset","true");
					  dom.setAttribute("class","ui-listview ui-listview-inset ui-corner-all ui-shadow");
					  var queryed=query(searchtext);
					  for(var i=0;i<queryed.length;i++){
						  var name=document.createElement("li");
						  name.setAttribute("data-role","list-divider");
						  name.setAttribute("role","heading");
						  name.setAttribute("class","ui-li-divider ui-bar-inherit ui-first-child");
						  name.appendChild(document.createTextNode(tree[queryed[i].id].name+"("+(tree[queryed[i].id].id+"0000000000000000000").substr(0,19)+")"));
						  dom.appendChild(name);
						  //var bm=document.createElement("li");
						  //bm.setAttribute("style","white-space:pre-wrap");
						  //bm.setAttribute("class","ui-li-static ui-body-inherit");
						  //bm.appendChild(document.createTextNode((tree[i].id+"0000000000000000000").substr(0,19)));
						  //dom.appendChild(bm);
						  var description=document.createElement("li");
						  description.setAttribute("class","ui-li-static ui-body-inherit");
						  description.setAttribute("style","white-space:pre-wrap");
						  description.appendChild(document.createTextNode(tree[queryed[i].id].description));
						  dom.appendChild(description);
						  var sl=tree[queryed[i].id].sl;
						  var zc=tree[queryed[i].id].zc;
						var tj=tree[queryed[i].id].tj;
						var finger=tree[queryed[i].id].parent;
						while(sl.length==0&&zc.length==0&&tj.length==0&&finger!=0&&finger!=null){
							sl=tree[finger].sl;
							zc=tree[finger].zc;
							tj=tree[finger].tj;
							finger=tree[finger].parent;
						}
						/*
						if(sl.length!=0){
							var hsl=document.createElement("li");
							hsl.setAttribute("class","ui-li-static ui-body-inherit");
							  hsl.setAttribute("style","white-space:pre-wrap");
							  hsl.appendChild(document.createTextNode(sl));
							  dom.appendChild(hsl);
						  }*/
						  if(zc.length!=0){
							  var hzc=document.createElement("li");
							hzc.setAttribute("class","ui-li-static ui-body-inherit");
							  hzc.setAttribute("style","white-space:pre-wrap");
							  hzc.appendChild(document.createTextNode(zc));
							  dom.appendChild(hzc);
						  //}
						  //if(tj.length!=0){
							  var htj=document.createElement("li");
							htj.setAttribute("class","ui-li-static ui-body-inherit");
							  htj.setAttribute("style","white-space:pre-wrap");
							  htj.appendChild(document.createTextNode(tj));
							  dom.appendChild(htj);
						  }
					  }
					  $(dom).appendTo("#result").trigger("create");
					  var reg=eval("/"+searchtext+"/g");
					  $("#result").html($("#result").html().replace(reg,'<font color="#FF0000">'+searchtext+'</font>'));
					  //=document.getElementsByTagName('BODY')[0].innerHTML.replace(reg,'<font color="#FF0000">'+searchtext+'</font>');
					  jQuery.mobile.changePage("#resultpage");
			}
		}
		String.prototype.trim0=function(){
			var position=this.search(/(00)+\b/);
			return position!=-1?this.substr(0,position):this;
		};
		var tree=new Array();
		$(document).ready(function(){
			$.ajax({
				url:"bmb.txt",
				dataType:"text",
				error:function(){alert("下载数据出错！");},
				success:function(response){
					tree[0]=createNode("0","商品和服务税收分类与编码","国家税务总局公告2016年23号","");
					var line=response.split("\n");
					var index,last=0;
					for(var i=0;i<line.length;i++){
						index=i+1;
						var field=line[i].split("\t");
						tree[index]=createNode(field[0].trim0(),field[1],field[2],field[3],field[4],field[5],field[6]);
						if(tree[index].id.length==1){
							tree[index].parent=0;
							tree[tree[index].parent].child.push(index);
							last=index;
							continue;
						}
						while(tree[index].id.length!=tree[last].id.length&&tree[index].id.indexOf(tree[last].id)!=0){
							last=tree[last].parent;
						}
						if(tree[index].id.indexOf(tree[last].id)==0){
							tree[index].parent=last;
							tree[last].child.push(index);
							last=index;
						}else {
							tree[index].parent=tree[last].parent;
							tree[tree[index].parent].child.push(index);
							last=index;
						}
					}//end of for alert(root.length);
				}//end of success
			});
		});//end of $(document).ready(
		$(document).on("collapsibleexpand",function(event, ui){
			if($(event.target).find("div:first").attr("id")==undefined){
				var did=$(event.target).attr("id");
				var id=did.substr(1,did.length);
				var node=null;
				for(var i=0;i<tree.length;i++){
					if(tree[i].id==id){
						node=tree[i];
						break;
					}
				}
				if(node!=null){
					var tg=$(event.target).find("div:first");
					tg.attr("id","s"+id);
					var subset=document.createElement("div");
					subset.setAttribute("data-role","collapsible-set");
					for(var i=0;i<node.child.length;i++)
						 subset.appendChild(tree[node.child[i]].toDOM());
					$(subset).appendTo(tg).trigger("create");
				}
			}
		});
	</SCRIPT>
	
  </head>
  <body>
    <h1>Hello, world!</h1>

    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="js/bootstrap.min.js"></script>
  </body>
</html>